<template>
  <div class="user-list">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form
          :inline="true"
          :model="formInline"
          class="demo-form-inline"
          ref="form"
        >
          <el-form-item label="手机号" prop="phone">
            <el-input
              v-model="formInline.phone"
              placeholder="请输入手机号"
            ></el-input>
          </el-form-item>
          <!-- <el-form-item label="注册时间" prop="rangeDate">
            <el-time-picker
              is-range
              arrow-control
              v-model="formInline.rangeDate"
              range-separator="至"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              placeholder="选择时间范围"
            >
            </el-time-picker>
          </el-form-item> -->
          <el-form-item>
            <el-button type="primary" @click="onSubmit" :disabled="isLoading"
              >查询</el-button
            >
            <el-button @click="onReset" :disabled="isLoading">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table :data="users" style="width: 100%" v-loading="isLoading">
        <el-table-column prop="name" label="用户名"> </el-table-column>
        <el-table-column prop="phone" label="手机号"> </el-table-column>
        <el-table-column prop="createTime" label="注册时间"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleRole(scope.row)" type="text"
              >分配角色</el-button
            >
            <el-button
              size="mini"
              @click="handleResource(scope.row)"
              type="text"
              >分配资源</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="formInline.currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="formInline.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
        :disabled="isLoading"
      >
      </el-pagination>
    </el-card>

    <el-dialog title="分配角色" :visible.sync="dialogVisible" width="50%">
      <el-select v-model="roleIdList" multiple placeholder="请选择">
        <el-option
          v-for="item in roles"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        >
        </el-option>
      </el-select>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAllocRole">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import Vue from "vue";
import { getUserPages } from "@/services/user";
import { getAllRoles, allocateUserRoles, getUserRole } from "@/services/role";
import { Form } from "element-ui";
export default Vue.extend({
  name: "UserListIndex",
  data() {
    return {
      users: [],
      formInline: {
        currentPage: 1,
        pageSize: 5,
        phone: "",
        userId: 0,
        startCreateTime: "",
        endCreateTime: "",
        rangeDate: [],
      },
      isLoading: false,
      totalCount: 0,
      dialogVisible: false,
      roles: [],
      roleIdList: [],
      currentUser: {},
    };
  },
  created() {
    this.loadGetUserPages();
  },
  methods: {
    onSubmit() {
      this.formInline.currentPage = 1;
      this.loadGetUserPages();
    },
    onReset() {
      (this.$refs.form as Form).resetFields();
      console.log(this.$refs.form);
      this.loadGetUserPages();
    },
    async handleRole(row: any) {
      this.dialogVisible = true;
      this.currentUser = row;
      const { data } = await getAllRoles();
      if (data.code === "000000") {
        this.roles = data.data;
      }
      const { data: dataUser } = await getUserRole(
        (this.currentUser as any).id
      );
      this.roleIdList = dataUser.data.map((item: any) => item.id);
    },
    async loadGetUserPages() {
      const { data } = await getUserPages(this.formInline);
      if (data.code === "000000") {
        this.users = data.data.records;
        this.totalCount = data.data.total;
      }
    },
    async handleAllocRole() {
      const { data } = await allocateUserRoles({
        userId: (this.currentUser as any).id,
        roleIdList: this.roleIdList,
      });
      if (data.code === "000000") {
        this.$message.success("操作成功");
        this.dialogVisible = false;
      }
    },
    handleSizeChange(val: number) {
      this.formInline.pageSize = val;
      this.formInline.currentPage = 1;
      this.loadGetUserPages();
    },
    handleCurrentChange(val: number) {
      this.formInline.currentPage = val;
      this.loadGetUserPages();
    },
  },
});
</script>

<style lang="sass" scoped>
</style>